/*
A better way to print a matrix
*/

#include <opencv2/opencv.hpp>
#include <iostream>

template <class T> void print_matrix(const cv::SparseMat_<T>* sm)
{
	cv::SparseMatConstIterator_<T> it = sm->begin();
	cv::SparseMatConstIterator_<T> it_end = sm->end();

	for (; it != it_end; ++it) {
		const typename cv::SparseMat_<T>::Node* node = it.node();
		std::cout << "( " << node->idx[0] << ", " << node->idx[1] << " ) = " << *it << std::endl;
	}
}


int main(int argc, char**  argv)
{
	int size[] = {10, 10};
    cv::SparseMat sm(2, size, CV_32F);
	// cv::SparseMat_<float> sm(ndims, size);	// 用该实例调研函数时，不需要进行指针转换

    for(int i = 0; i < 10; i++){
        int idx[2];
        // 此处应该需要的是0-1之间的数
        idx[0] = size[0] * (uint)rand() % 10;
        idx[1] = size[1] * (uint)rand() % 10;
        
        sm.ref<float>(idx) += 1.0f;
    }

	print_matrix<float>((cv::SparseMat_<float>*)&sm);

	return 0;
}